package com.ht.medicaltreatment.activity

import android.content.Intent
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.githang.statusbar.StatusBarCompat

import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.OrderDetailAdapter
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import com.ht.medicaltreatment.viewmodel.OrderDetailViewModel
import kotlinx.android.synthetic.main.order_detail_layout.imgIcon
import kotlinx.android.synthetic.main.order_detail_layout.recyclerView
import kotlinx.android.synthetic.main.order_detail_layout.tvOrderCode
import kotlinx.android.synthetic.main.order_detail_layout.tvOrderName
import kotlinx.android.synthetic.main.order_detail_layout.tvProgressTotal
import kotlinx.android.synthetic.main.order_detail_layout.tvRemarks
import kotlinx.android.synthetic.main.order_detail_layout.tvViewStubProgress

/**
 * 订单详情
 */
class OrderDetailActivity : BaseActivity() {
    private var isOpen = false
    private val uids = arrayListOf<String>()
    private var orderId:Int = 0

    private lateinit var adapter: OrderDetailAdapter
    private val model by viewModels<OrderDetailViewModel>()

    override fun getLayoutId() = R.layout.order_detail_layout

    override fun init() {
        StatusBarCompat.setStatusBarColor(this,getColor(R.color.white),true)
        orderId = intent.getIntExtra("id",0)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = OrderDetailAdapter().apply { adapter = this }
        ScanUtils.init()
        imgIcon.setOnClickListener {
            if(!isOpen){
                //如果没有打开的话，点击打开
                imgIcon.setBackgroundResource(R.drawable.icon_blue_nfc)
                startScan()
            }else{
                ScanUtils.close()
                imgIcon.setBackgroundResource(R.drawable.icon_gray_nfc)
            }
            isOpen = !isOpen
        }

        initData()
    }

    override fun onRestart() {
        super.onRestart()
        initData()
    }

    private fun initData() {
        showProgress()
        model.orderDetail("$orderId"){
            tvOrderName.text = it.transportOrder.orderName
            tvOrderCode.text = it.transportOrder.orderNumber
            tvRemarks.text = it.transportOrder.remarks
            adapter.setNewInstance(it.transportRfidSampleBoxList)
            var count = 0
            uids.clear()
            it.transportRfidSampleBoxList.forEach {
                if(it.transportStatus == 1){
                    count++
                }else{
                    uids.add(it.rfidCode)
                }
            }
            tvViewStubProgress.text = "$count"
            tvProgressTotal.text = "/${it.transportRfidSampleBoxList.size}"
            dismissProgress()
        }
    }

    private fun startScan(){
        ScanUtils.open {
            SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
            if(uids.contains(it)){
                ScanUtils.close()
                imgIcon.setBackgroundResource(R.drawable.icon_gray_nfc)
                isOpen = false
                val intent = Intent(this@OrderDetailActivity,ScanActivity::class.java)
                intent.putExtra("uid",it)
                intent.putExtra("orderId",orderId)
                startActivity(intent)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.close()
    }
}